/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.analysisresult.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.modules.analysisresult.entity.GrassAnalysisResult;
import com.longview.io.modules.analysisresult.service.GrassAnalysisResultService;

/**
 * 分析结果Controller
 * @author zf
 * @version 2018-04-24
 */
@Controller
@RequestMapping(value = "${adminPath}/analysisresult/grassAnalysisResult")
public class GrassAnalysisResultController extends BaseController {

	@Autowired
	private GrassAnalysisResultService grassAnalysisResultService;
	
	@ModelAttribute
	public GrassAnalysisResult get(@RequestParam(required=false) String id) {
		GrassAnalysisResult entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = grassAnalysisResultService.get(id);
		}
		if (entity == null){
			entity = new GrassAnalysisResult();
		}
		return entity;
	}
	
	@RequiresPermissions("analysisresult:grassAnalysisResult:view")
	@RequestMapping(value = {"list", ""})
	public String list(GrassAnalysisResult grassAnalysisResult, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GrassAnalysisResult> page = grassAnalysisResultService.findPage(new Page<GrassAnalysisResult>(request, response), grassAnalysisResult); 
		model.addAttribute("page", page);
		return "modules/analysisresult/grassAnalysisResultList";
	}

	@RequiresPermissions("analysisresult:grassAnalysisResult:view")
	@RequestMapping(value = "form")
	public String form(GrassAnalysisResult grassAnalysisResult, Model model) {
		model.addAttribute("grassAnalysisResult", grassAnalysisResult);
		return "modules/analysisresult/grassAnalysisResultForm";
	}

	@RequiresPermissions("analysisresult:grassAnalysisResult:edit")
	@RequestMapping(value = "save")
	public String save(GrassAnalysisResult grassAnalysisResult, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, grassAnalysisResult)){
			return form(grassAnalysisResult, model);
		}
		grassAnalysisResultService.save(grassAnalysisResult);
		addMessage(redirectAttributes, "保存分析结果成功");
		return "redirect:"+Global.getAdminPath()+"/analysisresult/grassAnalysisResult/?repage";
	}
	
	@RequiresPermissions("analysisresult:grassAnalysisResult:edit")
	@RequestMapping(value = "delete")
	public String delete(GrassAnalysisResult grassAnalysisResult, RedirectAttributes redirectAttributes) {
		grassAnalysisResultService.delete(grassAnalysisResult);
		addMessage(redirectAttributes, "删除分析结果成功");
		return "redirect:"+Global.getAdminPath()+"/analysisresult/grassAnalysisResult/?repage";
	}

}